package com.ggh.jinjilive.ui.mall.shopping;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.mall.goods.thegoods.TheGoodsAddressMobile;
import com.ggh.jinjilive.mobile.mall.shopping.ShopMessageMoblie;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ShopPingCarViewModel extends BaseViewModel {
    //购物车列表
    public MutableLiveData<List<ShopMessageMoblie>> mShopMessageMoblieList = new MutableLiveData<>();
    //CID
    public ObservableField<Integer> mCID = new ObservableField<>();

    public ShopPingCarViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    /**
     * 获取购物车的所有商品
     */
    public void cartList(){
        Map<String, Object> map = new HashMap<>();
        map.put("cid",mCID.get());
        addSubscribe(apiCallInterface.cartList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<ShopMessageMoblie>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<ShopMessageMoblie> shopMessageMoblieList = (List<ShopMessageMoblie>) resultEntity.getData();
                mShopMessageMoblieList.setValue(shopMessageMoblieList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }

    /**
     * 删除购物车商品
     * @param deleteList
     */
    public void delCart(String deleteList){
        Map<String, Object> map = new HashMap<>();
        map.put("ids",deleteList);
        addSubscribe(apiCallInterface.delCart(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                /**
                 * 获取购物车的所有商品
                 */
                cartList();
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }
}
